/************************************************************************

								Heroes IV
					  Copyright 2000, The 3DO Company

	------------------------------------------------------------------
  						     ai_town_data_cache.cpp

	$Header: /heroes4/ai_town_data_cache.h $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"
#include "ai_town_data_cache.h"

#include "ai_army_data_cache.h"
#include "adventure_object.h"
#include "creature_array.h"

void t_ai_town_data_cache::inc_threat( t_town* town, t_creature_array* army ) 
{
	t_threat_element element;
	t_threat_map::value_type insert_element( army, element );
	std::pair<t_threat_map::iterator, bool> insert_results = m_threat_map.insert( insert_element );
	if (insert_results.second) {
		army->get_ai_data_cache()->inc_threat( town );
		insert_results.first->second.object = army->get_adventure_object();
		insert_results.first->second.movement = army->get_movement();
	}
}

void t_ai_town_data_cache::get_threat_list( t_threat_list& list, t_threat_list* invalidated ) const
{
	list.clear();
	t_threat_map::const_iterator	iter = m_threat_map.begin(),
									end = m_threat_map.end();
	while(iter != end) 
	{
		t_threat_element const& element = iter->second;
		t_creature_array* array = iter->first;
		if (!array->empty())
		{
			if ( invalidated && (array->get_movement() != element.movement) )
				invalidated->push_back( array );
			else
				list.push_back( array );
		}
		iter++;
	}
}
